﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace CookeryBook
{
    public partial class FormSelectIng : Form
    {
        FormIngredient formIngredient;
        OleDbConnection connec;
        public int typeAction;

        public FormSelectIng(String lineConnection)
        {
            typeAction = 0;
            connec = new OleDbConnection(lineConnection);
            formIngredient = new FormIngredient(lineConnection);
            InitializeComponent();
        }

        private void FormSelectIng_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cuisineDataSet.ingredient' table. You can move, or remove it, as needed.
            this.cmb_ingredient.DisplayMember = "Nom";
            this.cmb_ingredient.DataSource = SelectIng();
            ChangeMesure();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (VerifChamps())
            {
                typeAction = 1;
                this.Close();
            }
            
        }

        private void btn_annuler_Click(object sender, EventArgs e)
        {
            typeAction = 0;
            this.Close();
        }

        private void cmb_ingredient_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeMesure();
        }

        /// <summary>
        /// Procédure d'affichage de la mesure en fonction de l'ingrédient
        /// </summary>
        private void ChangeMesure()
        {
            if (cmb_ingredient.SelectedIndex > -1)
            {
                lbl_mesure.Text = Enum.GetName(typeof(Unite),((Ingredient)cmb_ingredient.SelectedItem).unite);
            }
        }

        private void btn_addIngredient_Click(object sender, EventArgs e)
        {
            formIngredient.ShowDialog();
        }


        private List<Ingredient> SelectIng()
        {
            List<Ingredient> ingredients = new List<Ingredient>();
            OleDbDataReader reader = null;
            String description;

            try
            {
                //Commande ajout ingrédient
                connec.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = connec;
                cmd.CommandText = "Select * from ingredient";
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    if (reader[2] == System.DBNull.Value)
                    {
                        description = "";
                    }
                    else
                    {
                        description = reader.GetString(2);
                    }

                    ingredients.Add(new Ingredient(reader.GetInt32(0), reader.GetString(1), description, (Unite)Enum.Parse(typeof(Unite), reader.GetString(3))));
                }
            }
            catch (OleDbException ex)
            {
                System.Console.WriteLine(ex.Errors);
            }
            finally
            {
                reader.Close();
                connec.Close();
            }


            return ingredients;
        }


        private bool VerifChamps()
        {
            return (cmb_ingredient.SelectedIndex > -1 && txt_quantite.Text.Trim() != "");
        }

        
        /*protected Recette_ingredient RecupRecetteIng()
        {
            Recette_ingredient recetteIngredient = new Recette_ingredient();
            recetteIngredient.ingredient = (Ingredient)cmb_ingredient.SelectedItem;
            recetteIngredient.quantite = double.Parse(txt_quantite.Text);
            return recetteIngredient;
        }*/
    }
}
